import java.math.BigDecimal;

import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.core.methods.response.EthTransaction;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;

/**
 * 转账交易写法2
 */
public class TransactionTest2 {

	public static void main(String[] args) throws Exception {
		Web3j web3j = Web3j.build(new HttpService());
		String ownAddress = "0x66222c3342317e1e2c6fa3b100cb8d390b36c0a0";
		String toAddress = "0x3ff38e6f3ed7ac7d120ebd006fe4f4c8b0abbf2a";
		Credentials credentials = Credentials.create("0x796b571964bf6df4a186706dda87547b21ecf77caef950ad091481a33602ccc4");
		TransactionReceipt transactionReceipt = Transfer.sendFunds(
                web3j, credentials, toAddress,
                BigDecimal.valueOf(2), Convert.Unit.ETHER).send();
		// .send()方法是同步的，他是等交易成功之后再返回；.sendAsync()方法是异步的，返回一个Future。
        System.out.println(transactionReceipt.getTransactionHash());
        // 返回本次交易的hash值：0xc360c037e035f4cf4be88ad0bc567ebf052c9479d5f17009927f85b92a17a978
        
        // 查询交易状态
        Request<?, EthTransaction> transRequest = web3j.ethGetTransactionByHash(transactionReceipt.getTransactionHash());
        System.out.println(transRequest.getId());
        Request<?, EthGetTransactionReceipt> receiptRequest = web3j.ethGetTransactionReceipt(transactionReceipt.getTransactionHash());
        System.out.println(receiptRequest.getId());
	}

}
